//
//  LogUtils.m
//  OCDemo
//
//  Created by zhumuzhi on 2021/7/30.
//

#import "LogUtils.h"

#pragma mark - LogUtilsFormatter

@interface LogUtilsFormatter ()

/**
 * 时间格式属性
 */
@property (nonatomic, strong) NSDateFormatter *dateFormatter;

@end

@implementation LogUtilsFormatter

+ (instancetype)shared {
    static LogUtilsFormatter *formatter = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        formatter = [[LogUtilsFormatter alloc] init];
    });
    return formatter;
}

- (instancetype)init {
    self = [super init];
    if (self) {
      self.dateFormatter = [[NSDateFormatter alloc] init];
      self.dateFormatter.dateFormat = @"yyyy-MM-dd HH:mm:ss:SSS";
    }
    return self;
}

- (void)log:(NSString *)logMessage type:(LogType)type {
    NSString *logMark = @"";
    switch (type) {
        case LogTypeError:
            logMark = @"‼️ Error";
            break;
        case LogTypeWarning:
            logMark = @"⚠️ Warning";
            break;
        case LogTypeInfo:
            logMark = @"ℹ️ Info";
            break;
        case LogTypeDebug:
            logMark = @"✅ Debug";
            break;
        case LogTypeVerbose:
            logMark = @"⚪ Verbose";
            break;
        default:
            break;
    }

  NSString *dateStr = [self.dateFormatter stringFromDate:[NSDate date]];
  NSString *finalMessage = [NSString stringWithFormat:@"%@ | %@ | %@", logMark, dateStr, logMessage];
  printf("%s", [finalMessage UTF8String]);
}

@end


#pragma mark - LogUtils

@implementation LogUtils

+ (void)logError:(NSString *)message {
    NSString *file = [NSString stringWithFormat:@"%s", [[[NSString stringWithUTF8String:__FILE__]lastPathComponent] UTF8String]];
    NSString *func = [NSString stringWithFormat:@"%s", __func__];
    NSString *line = [NSString stringWithFormat:@"%d", __LINE__];
    [LogUtils log:message type:LogTypeError file:file func:func line:line];
}

+ (void)logWarning:(NSString *)message {
    NSString *file = [NSString stringWithFormat:@"%s", [[[NSString stringWithUTF8String:__FILE__]lastPathComponent] UTF8String]];
    NSString *func = [NSString stringWithFormat:@"%s", __func__];
    NSString *line = [NSString stringWithFormat:@"%d", __LINE__];
    [LogUtils log:message type:LogTypeWarning file:file func:func line:line];
}

+ (void)logInfo:(NSString *)message {
    NSString *file = [NSString stringWithFormat:@"%s", [[[NSString stringWithUTF8String:__FILE__]lastPathComponent] UTF8String]];
    NSString *func = [NSString stringWithFormat:@"%s", __func__];
    NSString *line = [NSString stringWithFormat:@"%d", __LINE__];
    [LogUtils log:message type:LogTypeInfo file:file func:func line:line];
}

+ (void)logDebug:(NSString *)message {
    NSString *file = [NSString stringWithFormat:@"%s", [[[NSString stringWithUTF8String:__FILE__]lastPathComponent] UTF8String]];
    NSString *func = [NSString stringWithFormat:@"%s", __func__];
    NSString *line = [NSString stringWithFormat:@"%d", __LINE__];
    [LogUtils log:message type:LogTypeDebug file:file func:func line:line];
}

+ (void)logVerbose:(NSString *)message {
    NSString *file = [NSString stringWithFormat:@"%s", [[[NSString stringWithUTF8String:__FILE__]lastPathComponent] UTF8String]];
    NSString *func = [NSString stringWithFormat:@"%s", __func__];
    NSString *line = [NSString stringWithFormat:@"%d", __LINE__];
    [LogUtils log:message type:LogTypeVerbose file:file func:func line:line];
}

+ (void)log:(NSString *)log type:(LogType)LogType file:(NSString *)file func:(NSString *)func line:(NSString *)line {
//    if(shouldLog) {
        NSString *formattedMsg = [NSString stringWithFormat:@"file:%@ | func:%@ | line:%@ | log:%@", file, func, line, log];
        LogUtilsFormatter *formatter = [LogUtilsFormatter shared];
    
        [formatter log:formattedMsg type:LogType];
//    }
}

@end


